import {
	SearchFormActionType,
	ICtrlDataAction,
	ICtrlEvent,
	ISearchFormAbility,
	ISearchFormControllerParams,
	ISearchFormModel,
	ISearchFormStore,
	IParam,
} from '@/core';
import { QXEvent } from 'qx-util';
import { PropType } from 'vue';
import {
	getCtrlClassNames,
	useCtrlAction,
	useCtrlBasic,
	useCtrlControllerParams,
	useCtrlProps,
} from '../use-ctrl';

/**
 * 表单输入参数
 *
 * @export
 * @return {*}
 */
export function useFormProps() {
	return {
		...useCtrlProps(),
		openView: Function,
		newView: Function,
		actions: Object as PropType<ICtrlDataAction>,
	};
}

/**
 * 表单基础参数
 *
 * @export
 * @param {ISearchFormModel} model
 * @param {IParam} props
 * @return {*}
 */
export function useFormBasic(model: ISearchFormModel, props: IParam) {
	const getClassNames = (props: IParam) => {
		return getCtrlClassNames(model, props);
	};
	const useShow = () => {
		const formDetails = model.detailModel;
		if (formDetails && Object.values(formDetails).length > 0) {
			const result = Object.values(formDetails).find((item: IParam) => {
				return item.detailType === 'FORMITEM';
			});
			return result ? true : false;
		} else {
			return false;
		}
	};
	return {
		...useCtrlBasic(model, props),
		getClassNames,
		useShow,
	};
}

/**
 * 表单部件行为
 *
 * @export
 */
export function useFormAction(controller: any) {
	const handleSearch = (event: MouseEvent) => {
		controller.search();
	};
	const handleAdvSearch = (info: any) => {
		controller.reset();
	};
	const handleShrinkButtonClick = () => {
		controller.shrinkButtonClick();
	};
	return {
		...useCtrlAction(controller),
		handleSearch,
		handleAdvSearch,
		handleShrinkButtonClick,
	};
}

/**
 * 表单部件控制器
 *
 * @export
 */
export function useFormControllerParams<
	S extends ISearchFormStore,
	M extends ISearchFormModel
>(
	model: M,
	props: any,
	evt: QXEvent<ICtrlEvent<SearchFormActionType, ISearchFormAbility>>,
	otherParams: IParam = {}
): ISearchFormControllerParams<SearchFormActionType, ISearchFormAbility> {
	return {
		...useCtrlControllerParams<S, M>(model, props, evt, otherParams),
		formValidate: async () => {
			return await otherParams.searchForm.value.formValidate();
		},
		formValidateFields: async (name: string[]) => {
			return await otherParams.searchForm.value.formValidateFields(name);
		},
		actions: props.actions,
		openView: props.openView,
		newView: props.newView,
	};
}
